package com.broada.component.flexcom.entity
{
	import com.broada.component.flexcom.manager.MapManager;
	import com.degrafa.GeometryGroup;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	
	import flash.events.MouseEvent;

	public class AreaGroup
	{
		public var id:String;
		public var group:GeometryGroup;
		private var areaChildren:Array;
		
		public function AreaGroup(gid:String)
		{
			id = gid;
			group = new GeometryGroup();
		}
		
		public function addArea(area:AreaItem):void{
			if(areaChildren == null){
				areaChildren = new Array();
			}
			areaChildren.push(area.id);
			area.group.target = group;
			group.geometryCollection.addItem(area.group);
		}
		
		public function set zoomEnabled(value:Boolean):void{
			if(value){
				group.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
				group.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			}else{
				group.removeEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
				group.removeEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			}
		}
		
		protected function mouseOverHandler(evt:MouseEvent):void{
			for each(var aid:String in areaChildren){
				var item:Object = MapManager.getInstance().getArea(aid);
				if(item != null && item is AreaItem){
					var ai:AreaItem = item as AreaItem;
					ai.assemblePoints();
				}
			}
			group.target.setChildIndex(group, group.target.numChildren - 1);
			TweenLite.to(group, 1,{scaleX:1.3, scaleY:1.3, x:-evt.stageX/5, y:-evt.stageY/5});
			TweenMax.to(group, 1, {glowFilter:{color:0xff0000, alpha:1, blurX:60, blurY:60, strength:1.5}});
		}
		
		protected function mouseOutHandler(evt:MouseEvent):void{
			TweenMax.to(group, 1, {glowFilter:{remove:true}});
			TweenLite.to(group, 1,{scaleX:1, scaleY:1, x:0, y:0, onComplete:scaleCompleteHandler});
		}
		
		private function scaleCompleteHandler():void{
			for each(var aid:String in areaChildren){
				var item:Object = MapManager.getInstance().getArea(aid);
				if(item != null && item is AreaItem){
					var ai:AreaItem = item as AreaItem;
					ai.splitPoints();
				}
			}
			group.target.setChildIndex(group, 0);
		}
	}
}